package com.foreveross.taskservice.application.impl.Util;

import java.util.List;
import java.util.Set;

import com.foreveross.taskservice.common.model.AbstractAction;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
import com.foreveross.taskservice.common.model.TaskAction;
import com.foreveross.taskservice.common.model.airfreight.AirFreightChannelModel;
import com.foreveross.taskservice.common.model.airfreight.AirFreightTaskModel;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.infra.entity.DetailInfoEntity;
import com.foreveross.taskservice.infra.entity.StaticHistoryHbaseEntity;
import com.google.common.collect.Lists;

/**
 * Hbase model转为Entity
 * @return
 */

public class ModelTranEntityUtil {
	
	public List<AbstractTaskModel> convertEntity(List<StaticHistoryHbaseEntity> hModsE,int pageLen,int pageIndex,GradChannelEntity channel){
		List<AbstractTaskModel> lists=Lists.newArrayList();
		if(hModsE.size()>0){
			hModsE=hModsE.subList(pageIndex*pageLen, (pageIndex+1)*pageLen);//Hbase的虚拟分页
			for(StaticHistoryHbaseEntity model:hModsE){
				AirFreightTaskModel vo=new AirFreightTaskModel();
				AirFreightChannelModel channelModel=new AirFreightChannelModel();
				channelModel.setHbaseKey(channel.getCode());
				channelModel.setId(channel.getId());
				channelModel.setName(channel.getName());
				channelModel.setHbaseKey(channel.getHbaseKey());
				vo.setChannel(channelModel);
				
				vo.setStatus(model.getStatus());
				vo.setFlightDate(model.getFlightDate());
				vo.setPriority(model.getPriority());
				vo.setReturnGrabDate(model.getReturnFlightDate());
				vo.setId(model.getTaskId());
				vo.setCreateTime(model.getCreateTime());
				vo.setFromCity(model.getFromCity());
				vo.setFromCityName(model.getFromCityName());
				vo.setToCity(model.getToCity());
				vo.setToCityName(model.getToCityName());
				vo.setStartTime(model.getStartTime());
				vo.setCrawlCount(model.getCrawlCount());
				vo.setExcuteCount(model.getExcuteCount());
				vo.setPriority(model.getPriority());
				vo.setSourceRowkey(model.getRowKey());
				vo.setEndTime(model.getEndTime());
				
				Set<DetailInfoEntity> detailSets=model.getDetails();
				//存入模型
				if(detailSets.size()>0){
					for(DetailInfoEntity dent:detailSets){
						AbstractAction te=new TaskAction(dent.getActionType(),dent.getTimestampStr(),dent.getRemark());
						vo.getActions().add(te);//还需要排序
					}
				}
				lists.add(vo);
				/*				
				Set<HBaseCellModel> tmpCells= model.getCells();
				AirFreightTaskModel vo=new AirFreightTaskModel();
				int actionType=0;
				long timestamp=0l;
				String remark="",timestampStr="";
				AirFreightChannelModel channelModel=new AirFreightChannelModel();
				channelModel.setCode(channel.getCode());
				channelModel.setId(channel.getId());
				channelModel.setName(channel.getName());
				vo.setChannel(channelModel);
				for (HBaseCellModel cell : tmpCells) {
					String family = cell.getFamily();
					String qualifier = cell.getQualifier();
					String value=cell.getValue();
					if(family.equals("crawlInfos")){//表示抓取基本信息
						if(qualifier.equals("toCity")){
							vo.setToCity(value);
						}else if(qualifier.equals("fromCity")){
							vo.setFromCity(value);
						}else if(qualifier.equals("toCityName")){
							vo.setToCityName(value);
						}else if(qualifier.equals("fromCityName")){
							vo.setFromCityName(value);
						}else if(qualifier.equals("createTime")){
							vo.setCreateTime(DateUtil.StringToDate("yyyy-MM-dd HH:mm:ss", Long.parseLong(value)));
						}else if(qualifier.equals("taskId")){
							vo.setId(value);
						}else if(qualifier.equals("flightDate")){
							vo.setFlightDate(value);
						}else if(qualifier.equals("returnFlightDate")){
							vo.setReturnGrabDate(value);
						}else if(qualifier.equals("priority")){
							vo.setPriority(Integer.parseInt(value));
						}else if(qualifier.equals("status")){
							vo.setStatus(Integer.parseInt(value));
						}else if(qualifier.equals("startTime")){
							vo.setStartTime(DateUtil.StringToDate("yyyy-MM-dd HH:mm:ss", value));
						}else if(qualifier.equals("endTime")){
							vo.setEndTime(DateUtil.StringToDate("yyyy-MM-dd HH:mm:ss", value));
						}else if(qualifier.equals("rowKey")){
							vo.setSourceRowkey(value);
						}else if(qualifier.equals("priority")){
							vo.setPriority(Integer.parseInt(value));
						}else if(qualifier.equals("excuteCount")){
							vo.setExcuteCount(Integer.parseInt(value));
						}else if(qualifier.equals("crawlCount")){
							vo.setCrawlCount(Integer.parseInt(value));
						}
					}else{//抓取详情信息
						if(qualifier.startsWith("timestampStr")){
							timestampStr=value;
						}else if(qualifier.startsWith("timestamp")){
							timestamp=Long.parseLong(value);
						}else if(qualifier.startsWith("remark")){
							remark=value;
						}else if(qualifier.startsWith("actionType")){
							actionType=Integer.parseInt(value);
						}
					}
				}
				//存入模型
				AbstractAction te=new TaskAction(actionType,timestampStr,remark);
				vo.getActions().add(te);//还需要排序
				lists.add(vo);
			*/
			}
			return lists;
		}
		
		return null;
	}
	
}
